package com.gewu.teday.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.gewu.teday.R
import com.gewu.teday.bean.ItemIedaBenans
import com.gewu.teday.fragmnet.ZhengTiFragment
import kotlinx.android.parcel.Parcelize

class IdeaListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var list = ArrayList<ItemIedaBenans>()
    var onclicks:Onclick?=null

    fun setOnClick(onclick: Onclick){
         this.onclicks = onclick
    }

    fun addList(itemBenan: ItemIedaBenans) {
        list.add(itemBenan)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate =
            LayoutInflater.from(parent.context).inflate(R.layout.list_item_idea, parent, false)
        return Hoder(inflate)
    }


    interface Onclick {
        fun showDialog(index:Int)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val hoder = holder as Hoder
        if (position == 0) {
            hoder.zuopin?.text = "作品"
            hoder.shouyi?.text = "创作收益"
            hoder.jichu?.text = "基础收益"
            hoder.image?.visibility = View.GONE
            hoder.paixu?.visibility = View.VISIBLE
        } else {
            hoder.zuopin?.text = list[position - 1].zuopin
            hoder.shouyi?.text = list[position - 1].shouyi
            hoder.jichu?.text = list[position - 1].jichu
            hoder.image?.visibility = View.VISIBLE
            hoder.paixu?.visibility = View.GONE


            hoder.layout?.setOnLongClickListener(object :View.OnLongClickListener {
                @SuppressLint("NewApi")
                override fun onLongClick(p0: View?): Boolean {
                    list.removeAt(position - 1)
                    onclicks?.showDialog(position-1)
                    return true
                }
            })
        }
    }

    override fun getItemCount(): Int = list.size+1

    class Hoder(inflate: View?) : RecyclerView.ViewHolder(inflate!!) {

        val zuopin: TextView? = inflate?.findViewById<TextView>(R.id.zuopin)
        var shouyi: TextView? = inflate?.findViewById<TextView>(R.id.shouyi)
        var image: ImageView? = inflate?.findViewById<ImageView>(R.id.image)
        var paixu: ImageView? = inflate?.findViewById<ImageView>(R.id.paixu)
        var jichu: TextView? = inflate?.findViewById<TextView>(R.id.jichu)
        var layout: RelativeLayout? = inflate?.findViewById<RelativeLayout>(R.id.layout)
    }
}

@Parcelize
data class IedaListBean (var list: ArrayList<ItemIedaBenans>):Parcelable

